package com.ants.modules.wxManage.controller;

import com.ants.common.annotation.AutoLog;
import com.ants.common.system.query.QueryGenerator;
import com.ants.common.system.result.Result;
import com.ants.modules.ArticleManage.entity.ArticleManage;
import com.ants.modules.ArticleManage.service.ArticleManageService;
import com.ants.modules.wxManage.entity.AntsWxSwiper;
import com.ants.modules.wxManage.service.AntsWxSwiperService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Map;

/**
 * TODO
 * Author Chen
 * Date   2021/9/7 18:44
 */

@Slf4j
@Api(tags = "微信小程序-首页轮播图")
@RestController
@RequestMapping("/antsWxSwiper")
public class AntsWxSwiperController {

    @Autowired
    AntsWxSwiperService entityService;
    @Autowired
    ArticleManageService articleManageService;

    @AutoLog(value = "微信小程序-首页轮播图-列表")
    @ApiOperation(value = "微信小程序-首页轮播图-列表", notes = "微信小程序-首页轮播图-列表")
    @GetMapping("/list")
    public Result<?> queryPageList(AntsWxSwiper entity,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<AntsWxSwiper> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        queryWrapper.orderByAsc("sort_no");
        Page<AntsWxSwiper> page = new Page<AntsWxSwiper>(pageNo, pageSize);
        IPage<AntsWxSwiper> pageList = entityService.page(page, queryWrapper);
        pageList.getRecords().forEach(e -> {
            ArticleManage articleManage = articleManageService.getById(e.getArticleId());
            e.setCover(articleManage.getCover());
        });
        return Result.ok(pageList);
    }

    @AutoLog(value = "微信小程序-首页轮播图-列表")
    @ApiOperation(value = "微信小程序-首页轮播图-列表", notes = "微信小程序-首页轮播图-列表")
    @GetMapping("/selectArticle")
    public Result<?> selectArticle(@RequestParam(required = false) String title,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        IPage<Map<String, String>> list = entityService.selectArticle(title, pageNo, pageSize);
        return Result.ok(list);
    }

    /**
     * 添加
     *
     * @param entity
     * @return
     */
    @AutoLog(value = "微信小程序-首页轮播图-添加")
    @ApiOperation(value = "微信小程序-首页轮播图-添加", notes = "微信小程序-首页轮播图-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody AntsWxSwiper entity) {
        entityService.save(entity);
        return Result.ok("添加成功！");
    }

    /**
     * @param id
     * @return
     * @功能：删除
     */
    @DeleteMapping("/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        boolean ok = entityService.removeById(id);
        if (ok) {
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }

    /**
     * @param ids
     * @return
     * @功能：批量删除
     */
    @DeleteMapping("/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        boolean ok = entityService.removeByIds(Arrays.asList(ids.split(",")));
        if (ok) {
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }
}
